package fairygui.editor.handlers
{
   import fairygui.editor.Consts;
   import fairygui.editor.gui.EPackageItem;
   import fairygui.editor.gui.EUIPackage;
   import fairygui.editor.gui.EUIProject;
   import fairygui.editor.utils.UtilsFile;
   import fairygui.editor.utils.UtilsStr;
   import flash.filesystem.File;
   import flash.system.System;
   
   public class StringsHandler
   {
       
      
      private var _result:Array;
      
      private var _refValues:Object;
      
      public function StringsHandler()
      {
         super();
      }
      
      public function exportStrings(param1:Vector.<EUIPackage>, param2:File, param3:Boolean) : void
      {
         var _loc5_:EUIPackage = null;
         var _loc6_:Vector.<EPackageItem> = null;
         var _loc7_:EPackageItem = null;
         var _loc8_:XML = null;
         var _loc9_:Object = null;
         if(param3 && param2.exists)
         {
            this.readRefs(param2);
         }
         this._result = [];
         var _loc4_:XML = <resources/>;
         for each(_loc5_ in param1)
         {
            _loc5_.ensureOpen();
            _loc6_ = _loc5_.resources.concat();
            _loc6_.sort(this.compareItem);
            for each(_loc7_ in _loc6_)
            {
               if(_loc7_.type == EPackageItem.COMPONENT)
               {
                  this._result.length = 0;
                  this.analyseItem(_loc7_);
                  if(this._result.length > 0)
                  {
                     XML.ignoreComments = false;
                     _loc8_ = new XML("<!-- " + _loc7_.owner.name + " - " + _loc7_.name + " -->");
                     _loc4_.appendChild(_loc8_);
                     XML.ignoreComments = true;
                     for each(_loc9_ in this._result)
                     {
                        _loc8_ = <string/>;
                        _loc8_.@name = _loc9_.name;
                        _loc8_.@mz = _loc9_.mz;
                        _loc8_.appendChild(new XML(UtilsStr.encodeHTML(_loc9_.text)));
                        _loc4_.appendChild(_loc8_);
                     }
                  }
               }
            }
         }
         UtilsFile.saveXML(param2,_loc4_);
         System.disposeXML(_loc4_);
      }
      
      public function parseImport(param1:File, param2:EUIProject) : Vector.<String>
      {
         var _loc8_:XML = null;
         var _loc9_:String = null;
         var _loc10_:String = null;
         var _loc11_:int = 0;
         var _loc12_:EUIPackage = null;
         var _loc3_:Vector.<String> = new Vector.<String>();
         var _loc4_:XML = UtilsFile.loadXML(param1);
         var _loc5_:XMLList = _loc4_.string;
         var _loc6_:Object = {};
         var _loc7_:Vector.<String> = new Vector.<String>();
         for each(_loc8_ in _loc5_)
         {
            _loc9_ = _loc8_.@name;
            _loc10_ = _loc9_.substr(0,8);
            _loc11_ = _loc6_[_loc10_];
            if(_loc11_ == 0)
            {
               _loc7_.push(_loc10_);
            }
            _loc11_++;
            _loc6_[_loc10_] = _loc11_;
         }
         for each(_loc10_ in _loc7_)
         {
            _loc12_ = param2.getPackage(_loc10_);
            _loc3_.push(_loc12_.name + "(" + _loc6_[_loc10_] + ")");
         }
         return _loc3_;
      }
      
      public function importStrings(param1:File, param2:Vector.<String>, param3:EUIProject) : void
      {
         var cxml:XML = null;
         var dxml:XML = null;
         var pkgId:String = null;
         var coms:Object = null;
         var pkg:EUIPackage = null;
         var srcId:String = null;
         var values:Object = null;
         var pi:EPackageItem = null;
         var displayList:XMLList = null;
         var cnt:int = 0;
         var done:Boolean = false;
         var i:int = 0;
         var elementId:String = null;
         var fieldId:String = null;
         var text:String = null;
         var col:Object = null;
         var ename:String = null;
         var items:XMLList = null;
         var index:int = 0;
         var file:File = param1;
         var warnings:Vector.<String> = param2;
         var project:EUIProject = param3;
         var xml:XML = UtilsFile.loadXML(file);
         var strings:Object = this.parseXML(xml);
         for(pkgId in strings)
         {
            coms = strings[pkgId];
            pkg = project.getPackage(pkgId);
            for(srcId in coms)
            {
               values = coms[srcId];
               pi = pkg.getItem(srcId);
               if(pi == null)
               {
                  warnings.push(Consts.g.text188 + " " + srcId);
                  return;
               }
               xml = pkg.getComponentXML(pi,false);
               if(xml == null)
               {
                  warnings.push(Consts.g.text188 + " " + pi.name);
                  return;
               }
               displayList = xml.displayList.elements();
               cnt = values.length;
               i = 0;
               while(i < cnt)
               {
                  elementId = values[i + 1];
                  fieldId = values[i + 2];
                  text = values[i + 3];
                  done = false;
                  col = displayList.(@id == elementId);
                  if(col.length() == 0)
                  {
                     warnings.push(Consts.g.text188 + " " + values[i] + " " + text);
                  }
                  else
                  {
                     cxml = col[0];
                     ename = cxml.name().localName;
                     if(fieldId == "tips")
                     {
                        cxml.@tooltips = text;
                     }
                     else if(fieldId == "texts")
                     {
                        if((dxml = cxml.gearText[0]) != null)
                        {
                           dxml.@values = text;
                        }
                     }
                     else if(fieldId == "texts_def")
                     {
                        if((dxml = cxml.gearText[0]) != null)
                        {
                           dxml["default"] = text;
                        }
                     }
                     else
                     {
                        if(ename == "text" || ename == "richtext")
                        {
                           if(fieldId == "prompt")
                           {
                              cxml.@prompt = text;
                           }
                           else
                           {
                              cxml.@text = text;
                           }
                           done = true;
                        }
                        else if(ename == "list")
                        {
                           items = cxml.item;
                           index = int(fieldId);
                           if(index < items.length())
                           {
                              items[index].@title = text;
                              done = true;
                           }
                        }
                        else if(ename == "component")
                        {
                           if((dxml = cxml.Button[0]) != null)
                           {
                              if(fieldId == "0")
                              {
                                 dxml.@selectedTitle = text;
                              }
                              else
                              {
                                 dxml.@title = text;
                              }
                              done = true;
                           }
                           else if((dxml = cxml.Label[0]) != null)
                           {
                              if(fieldId == "prompt")
                              {
                                 dxml.@prompt = text;
                              }
                              else
                              {
                                 dxml.@title = text;
                              }
                              done = true;
                           }
                           else if((dxml = cxml.ComboBox[0]) != null)
                           {
                              if(!fieldId)
                              {
                                 dxml.@title = text;
                                 done = true;
                              }
                              else
                              {
                                 items = dxml.item;
                                 index = int(fieldId);
                                 if(index < items.length())
                                 {
                                    items[index].@title = text;
                                    done = true;
                                 }
                              }
                           }
                        }
                        if(!done)
                        {
                           warnings.push(Consts.g.text188 + " " + values[i] + " " + text);
                        }
                     }
                  }
                  i = i + 4;
               }
               UtilsFile.saveXML(pi.file,xml);
               pi.invalidate();
            }
         }
      }
      
      private function parseXML(param1:XML) : Object
      {
         var _loc4_:XML = null;
         var _loc5_:String = null;
         var _loc6_:String = null;
         var _loc7_:String = null;
         var _loc8_:String = null;
         var _loc9_:Array = null;
         var _loc10_:String = null;
         var _loc11_:String = null;
         var _loc12_:Object = null;
         var _loc13_:Array = null;
         var _loc2_:XMLList = param1.string;
         var _loc3_:Object = {};
         for each(_loc4_ in _loc2_)
         {
            _loc5_ = _loc4_.@name;
            _loc6_ = _loc4_.toString();
            _loc7_ = _loc5_.substr(0,8);
            _loc8_ = _loc5_.substr(8);
            _loc9_ = _loc8_.split("-");
            _loc8_ = _loc9_[0];
            _loc10_ = _loc9_[1];
            _loc11_ = _loc9_[2];
            _loc12_ = _loc3_[_loc7_];
            if(!_loc12_)
            {
               _loc12_ = {};
               _loc3_[_loc7_] = _loc12_;
            }
            _loc13_ = _loc12_[_loc8_];
            if(!_loc13_)
            {
               _loc13_ = [];
               _loc12_[_loc8_] = _loc13_;
            }
            _loc13_.push(_loc5_,_loc10_,_loc11_,_loc6_);
         }
         return _loc3_;
      }
      
      private function addResult(param1:EPackageItem, param2:String, param3:String, param4:String, param5:String) : void
      {
         var _loc8_:String = null;
         if(!this.validateText(param5))
         {
            return;
         }
         var _loc6_:Object = {};
         var _loc7_:String = param1.owner.id + param1.id + "-" + param2 + (!!param4?"-" + param4:"");
         _loc6_.name = _loc7_;
         _loc6_.mz = param3;
         if(this._refValues)
         {
            _loc8_ = this._refValues[_loc7_];
            if(_loc8_ != null)
            {
               param5 = _loc8_;
            }
         }
         _loc6_.text = param5;
         this._result.push(_loc6_);
      }
      
      private function analyseItem(param1:EPackageItem) : void
      {
         var col:Object = null;
         var cxml:XML = null;
         var dxml:XML = null;
         var exml:XML = null;
         var xml:XML = null;
         var ename:String = null;
         var elementId:String = null;
         var elementName:String = null;
         var items:XMLList = null;
         var j:int = 0;
         var pi:EPackageItem = param1;
         var file:File = pi.file;
         if(!file.exists)
         {
            return;
         }
         var str:String = UtilsFile.loadString(file);
         try
         {
            xml = new XML(str);
         }
         catch(err:Error)
         {
            return;
         }
         col = xml.displayList.elements();
         for each(cxml in col)
         {
            ename = cxml.name().localName;
            elementId = cxml.@id;
            elementName = cxml.@name;
            this.addResult(pi,elementId,elementName,"tips",cxml.@tooltips);
            if(ename == "text" || ename == "richtext")
            {
               this.addResult(pi,elementId,elementName,null,cxml.@text);
               this.addResult(pi,elementId,elementName,"prompt",cxml.@prompt);
            }
            else if(ename == "list")
            {
               items = cxml.item;
               j = 0;
               for each(exml in items)
               {
                  this.addResult(pi,elementId,elementName,"" + j,exml.@title);
                  j++;
               }
            }
            else if(ename == "component")
            {
               if((dxml = cxml.Button[0]) != null)
               {
                  this.addResult(pi,elementId,elementName,null,dxml.@title);
                  this.addResult(pi,elementId,elementName,"0",dxml.@selectedTitle);
               }
               else if((dxml = cxml.Label[0]) != null)
               {
                  this.addResult(pi,elementId,elementName,null,dxml.@title);
                  this.addResult(pi,elementId,elementName,"prompt",dxml.@prompt);
               }
               else if((dxml = cxml.ComboBox[0]) != null)
               {
                  this.addResult(pi,elementId,elementName,null,dxml.@title);
                  items = dxml.item;
                  j = 0;
                  for each(exml in items)
                  {
                     this.addResult(pi,elementId,elementName,"" + j,exml.@title);
                     j++;
                  }
               }
            }
            if((dxml = cxml.gearText[0]) != null)
            {
               str = dxml.@values;
               this.addResult(pi,elementId,elementName,"texts",str);
               str = dxml["default"];
               this.addResult(pi,elementId,elementName,"texts_def",str);
            }
         }
      }
      
      private function validateText(param1:String) : Boolean
      {
         var _loc5_:int = 0;
         if(!param1)
         {
            return false;
         }
         var _loc2_:int = param1.length;
         var _loc3_:Boolean = true;
         var _loc4_:int = 0;
         while(_loc4_ < _loc2_)
         {
            _loc5_ = param1.charCodeAt(_loc4_);
            if(_loc5_ >= 65)
            {
               _loc3_ = false;
            }
            _loc4_++;
         }
         return !_loc3_;
      }
      
      private function readRefs(param1:File) : void
      {
         var _loc4_:XML = null;
         var _loc5_:String = null;
         this._refValues = {};
         var _loc2_:XML = UtilsFile.loadXML(param1);
         var _loc3_:XMLList = _loc2_.string;
         for each(_loc4_ in _loc3_)
         {
            _loc5_ = _loc4_.@name;
            this._refValues[_loc5_] = _loc4_.toString();
         }
         System.disposeXML(_loc2_);
      }
      
      private function compareItem(param1:EPackageItem, param2:EPackageItem) : int
      {
         return param1.sortKey.localeCompare(param2.sortKey);
      }
   }
}
